package com.hiwhitley.leetcode.easy;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * Created by hiwhitley on 2017/3/25.
 */
public class ExpressionEvaluates {

    public static int evaluate(String s) {
        if (s == null || s.equals(""))
            return 0;
        char[] chars = s.toCharArray();
        int temp = 0;
        int cur = 0;
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < chars.length; i++) {
            cur = chars[i] - '0';
            if (cur >= 0 && cur <= 9) {
                temp = temp * 10 + cur;
            } else {
                queue.offer(temp);
                temp = 0;
            }
        }
        queue.offer(temp);
        int result = queue.poll();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '-') {
                result -= queue.poll();
            }
            if (chars[i] == '+') {
                result += queue.poll();
            }
            if (chars[i] == '*') {
                result *= queue.poll();
            }
        }
        return result;
    }

    public static int evaluate2(String input) {
        if (input == null || input.equals(""))
            return 0;
        char[] chars = input.toCharArray();
        int cur;
        int temp = 0;
        int result = 0;
        char preOperator = '+';
        for (int i = 0; i < chars.length; i++) {
            cur = chars[i] - '0';
            if (cur >= 0 && cur <= 9) {
                temp = temp * 10 + cur;
            } else {
                if (preOperator == '+') {
                    result += temp;
                }
                if (preOperator == '-') {
                    result -= temp;
                }
                if (preOperator == '*') {
                    result *= temp;
                }
                temp = 0;
                preOperator = chars[i];
            }
        }

        if (preOperator == '+') {
            result += temp;
        }
        if (preOperator == '-') {
            result -= temp;
        }
        if (preOperator == '*') {
            result *= temp;
        }
        return result;
    }

    public static void main(String[] args) {
        while (true) {
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            System.out.println(evaluate2(input));
        }
    }

}
